<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 01:21:39
 * @LastEditTime: 2023-10-31 15:50:12
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Traits;

use App\Models\ComprehensiveCar;
use App\Models\ComprehensiveCarDetails;
use App\Repositories\ComprehensiveCarRepository;
use Illuminate\Support\Facades\App;

trait CarTrait
    {
    use CarryCarDetailTrait;
    public function CarTraitDeleting()
        {
        $this->removeCarItem();
        $this->removeCarCarryDetail($this::class, $this->getCarSourceFieldKey(), $this->{$this->getCarSourceFieldKey()});
        }
    public function CarTraitCreated($model)
        {
        $quantity_field           = $model->getCarItemQuantityField();
        $auxiliary_quantity_field = $model->getCarItemAuxiliaryQuantityField();
        return $this->createCarCarryDetail(
            $model->material_id,
            $model->dyelot,
            $model->type_id,
            $model->lot_id,
            $model->color_id,
            $model->{$auxiliary_quantity_field},
            $model->{$quantity_field},
                $model::class,
            $this->getCarSourceFieldKey(),
            $model->{$this->getCarSourceFieldKey()}
        );
        }
    public function CarTraitUpdated($model)
        {
        return $this->CarTraitCreated($model);
        }
    /**
     * 获取车车模型
     * @return ComprehensiveCarRepository
     * @author sunkaiyuan
     */
    public function getCarRepository()
        {
        return App::make(ComprehensiveCarRepository::class);
        }
    /**
     * 车与本单关联的关键字
     * @var string
     * @author sunkaiyuan
     */
    private $_carKey = 'car_id';
    private $_car_source_field = 'id';
    private $_quantityField = 'quantity';
    private $_auxiliary_quantity_field = 'auxiliary_quantity';
    public function getCarItemQuantityField()
        {
        $key = $this->_quantityField;
        if (property_exists(__CLASS__, "quantityField"))
            $key = $this->quantityField;
        return $key;
        }
    public function getCarItemAuxiliaryQuantityField()
        {
        $key = $this->_auxiliary_quantity_field;
        if (property_exists(__CLASS__, "auxiliary_quantity_field"))
            $key = $this->auxiliary_quantity_field;
        return $key;
        }
    public function carItem()
        {
        $key = $this->getCarSourceFieldKey();
        return $this->hasOne(ComprehensiveCarDetails::class, "source_value", $key)->where(function ($query) use ($key)
            {
            $query->where("source_key", $key)
                ->where("source_model", $this::class);
            });
        }
    public function car()
        {
        return $this->belongsTo(ComprehensiveCar::class, $this->getCarKey());
        }
    /**
     * 获取车关联关键字段
     * @return mixed
     * @author sunkaiyuan
     */
    public function getCarSourceFieldKey()
        {
        $key = $this->_car_source_field;
        if (property_exists(__CLASS__, "carSourceField"))
            $key = $this->CarSourceField;
        return $key;
        }
    /**
     * 获取车关联关键字
     * @return mixed
     * @author sunkaiyuan
     */
    public function getCarKey()
        {
        $key = $this->_carKey;
        if (property_exists(__CLASS__, "carKey"))
            $key = $this->carkey;
        return $key;
        }
    /**
     * 新增派车，必须有明细
     * @param mixed $car_id
     * @return mixed
     * @author sunkaiyuan
     */
    public function addCar($source_addr, $target_addr, $code, $car_id = null)
        {
        $carRepos = $this->getCarRepository();

        return $this->details->filter(function ($item) use ($car_id, $carRepos, $source_addr, $code, $target_addr)
            {
            return $carRepos->addItem($source_addr, $target_addr, $code, $car_id, $item);
            });
        }
    /**
     * 删除派车单，必须有明细
     * @param mixed $car_id
     * @return mixed
     * @author sunkaiyuan
     */
    public function removeCar($car_id = null)
        {
        $carRepos = $this->getCarRepository();
        $car_id   = $this->_getCarId($car_id);
        if ($car_id === null) {
            return;
            }
        return $this->details->filter(function ($item) use ($car_id, $carRepos)
            {
            return $carRepos->removeItem($car_id, $item);
            });
        }
    /**
     * 增加派车明细，自己就是明细
     * @param mixed $car_id
     * @return mixed
     * @author sunkaiyuan
     */
    public function addCarItem($source_addr, $target_addr, $car_id = null, $code = null)
        {
        $car_id = $this->_getCarId($car_id);
        if ($car_id === null) {
            return;
            }

        $isnew = $this->getCarRepository()->addItem($source_addr, $target_addr, $code ?? $this->code, $car_id, $this);
        if ($isnew) {
            $detail            = $this->getCar_carry_detailAttribute($this::class, $this->getCarSourceFieldKey(), $this->{$this->getCarSourceFieldKey()});
            $detail->car_count = 0;
            $detail->save();
            }
        }
    /**
     * 获取车Id
     * @param mixed $car_id
     * @return mixed
     * @author sunkaiyuan
     */
    private function _getCarId($car_id = null)
        {
        return $car_id ?? $this->{$this->getCarKey()};
        }
    /**
     * 从派车明细中删除
     * @param mixed $car_id
     * @return mixed
     * @author sunkaiyuan
     */
    public function removeCarItem($car_id = null)
        {
        $detail = $this->getCar_carry_detailAttribute($this::class, $this->getCarSourceFieldKey(), $this->{$this->getCarSourceFieldKey()});
        if ($detail) {
            $detail->car_count = 1;
            $detail->save();
            }
        $car_id = $this->_getCarId($car_id);
        if ($car_id === null) {
            return;
            }
        return $this->getCarRepository()->removeItem($car_id, $this);
        }

    public function scopeAddCar($source_addr, $target_addr, $car_id = null)
        {
        return $this->get()->filter(function ($v) use ($car_id, $source_addr, $target_addr)
            {
            return $v->addCar($source_addr, $target_addr, $car_id);
            });
        }

    public function scopeAddCarItem($source_addr, $target_addr, $car_id = null, $code = null)
        {
        return $this->get()->filter(function ($v) use ($car_id, $source_addr, $target_addr, $code)
            {
            return $v->addCarItem($source_addr, $target_addr, $car_id, $code);
            });
        }
    public function scopeRemoveCarItem($car_id = null)
        {
        return $this->get()->filter(function ($v) use ($car_id)
            {
            return $v->removeCarItem($car_id);
            });
        }
    public function scoperemoveCar($car_id = null)
        {
        return $this->get()->filter(function ($v) use ($car_id)
            {
            return $v->removeCar($car_id);
            });
        }
    }